package com.xy.controller;

import cn.hutool.core.util.IdUtil;
import com.xy.redisson.RedissonLock;
import com.xy.redisson.annotation.DistributedLock;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * 基于注解方式加锁
 * @author xxy
 * @date 2022/1/6 12:37
 */
@Slf4j
@RestController
@RequestMapping("annotation")
public class AnnotationLockController {

    private final RedissonLock redissonLock;

    public AnnotationLockController(RedissonLock redissonLock) {
        this.redissonLock = redissonLock;
    }


    /**
     * 模拟库存
     */
    private static volatile Integer TOTAL = 10;



    @GetMapping("lock-decrease-stock")
    @DistributedLock(value = "goodId", leaseTime = 5)
    public String lockDecreaseStock() {
        if (TOTAL > 0){
            TOTAL--;
        }
        log.info("===注解模式=== 减完库存后,当前库存===" + TOTAL);
        return "========================================";
    }


}
